package com.instwall.callbase.tool;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by lk on 17-7-24.
 */

public class MD5Util {

    private static String publicKey = "3269049B602CA40CCA782EDD87A52590";
    private static final String ENCODE = "UTF-8";

    //    将字段名按字母表顺序排序，签名方式如下：
    //    MD5(secret_key_valuekey1value1key2value2key3value3…keynvaluensecret_key_value)
    //    注：secret_key_value 为双方公用的密钥
    //    所有签名参与项均需要使用UTF-8编码处理
    //    签名结果应以16进制方式展现
    public static String MD5sign(HashMap<String, String> hashMap) {
        String signSrc = publicKey;
        ArrayList<String> signFields = new ArrayList<>();
        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            String key = entry.getKey();
            signFields.add(key);
        }
        if (signFields != null && signFields.size() > 1) {
            Collections.sort(signFields);
        }
        for (int i = 0; i < signFields.size(); i++) {
            String key = signFields.get(i);
            signSrc = signSrc + key + hashMap.get(key);
        }
        signSrc = signSrc + publicKey;
        System.out.println("signSrc:" + signSrc);
        try {
            String digestMessage = getMessageDigest(signSrc).toUpperCase();
            System.out.println("md5sign:" + digestMessage);
            return digestMessage;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 功能：MD5加密
     *
     * @param strSrc 加密的源字符串
     * @return 加密串 长度32位(hex串)
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String getMessageDigest(String strSrc) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = null;
        String strDes = null;
        final String ALGO_MD5 = "MD5";

        byte[] bt = strSrc.getBytes(ENCODE);
        md = MessageDigest.getInstance(ALGO_MD5);
        md.update(bt);
        strDes = byte2Hex(md.digest());
        return strDes;
    }

    /**
     * 将字节数组转为HEX字符串(16进制串)
     *
     * @param bts 要转换的字节数组
     * @return 转换后的HEX串
     */
    public static String byte2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }

    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("store_id", "202112");
        hashMap.put("marketId", "01");
        String md5sign = MD5sign(hashMap).toUpperCase();
        System.out.println("md5sign:" + md5sign);
    }
}
